import { useCommonMessage } from "@/utils/discrete"
import { RequestOptions, ResponseData } from "../types"
import { isFunction } from "@/utils"

/**
 * 异常处理
 */
export function handleResponseData(response: ResponseData<any>, options: RequestOptions) {
    const { error, success } = useCommonMessage()
    if (response.status) {
        const { successFn } = options
        if (successFn === true) {
            success(response.message)
        } else if (isFunction(successFn)) {
            ;(successFn as Function)(response, options)
        }
    } else {
        const { errorFn } = options
        if (errorFn === true) {
            error(response.message)
        } else if (isFunction(errorFn)) {
            ;(errorFn as Function)(response, options)
        }
    }
    return response
}
